

var validator = new Validator(document.forms[0])


validator.config.info.className = 'red'

validator
  .hook({
    'fieldSucceed': function (field) {
     field.infoEl.className = 'green'
     console.log('成功' + field.name)
    },
    'fieldFailed': function (field) {
      field.infoEl.className = 'red'
      console.log('失败' + field.name)
    },
    'succeed': function () {
      console.log('通过')
    },
    'failed': function () {
      console.log('失败')
    },
   'error': function (err) {
      if (err) {
      console.log(err) 
      }
    }
  })
  .pattern({
  'isUsername': /^[0-9a-zA-z]+$/,
  'isPassword': /^[0-9a-zA-zsds]+$/,
})
  .async('is', function (field, msg, err, is) {
    return new Promise(function (resolve, reject) {
      field.infoMsg = '正在查询中...'
      field.infoEl.className = 'gray'
      setTimeout(function () {
        if (field.value === is) {
          resolve(msg)
        } else {
          reject(err)
        }
      }, 2000)
    })
  })
  .add('username', [
    ['notEmpty', '不能为空'],
    ['minLength', 3, '至少3个字符'],
    ['maxLength', 10, '最多10个字符'],
    ['test', 'isUsername', '用户名只能包含数字、字符或下划线'],
  ])

  .add('password', [
    ['notEmpty', '不能为空'],
    ['minLength', 6, '至少6个字符'],
    ['maxLength', 16, '最多16个字符'],
    ['test', 'isUsername', '密码只能包含数字、字符'],
    ['is', '123456', '异步成功', '异步失败']
  ])


  .add('email', [
    ['test', 'email', '格式不正确']
  ])

  .add('phone', [
    ['test', 'phone', '格式不正确']
  ])
  .oninput()
  .onsubmit(function () {
     document.querySelector('.text').innerText = JSON.stringify(validator.getData('repeatPassword'))
  })

